/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var detectCycle = function(head) {
    if(head===null||head.next===null)
    {
     return null
    }
    let p1=head
    let p2=head
    while(p2!==null&&p2.next!==null)
    {
        p1=p1.next
        p2=p2.next.next
        if(p2===p1)
        {
            let ptr=head
            while(ptr!==p1)
            {
                ptr=ptr.next
                p1=p1.next
            }
            return ptr
        }
    }
    return null



};